package com.zr.planClass.service.impl;

import com.zr.planClass.mapper.InquiryMapper;
import com.zr.planClass.model.inquiry.*;
import com.zr.planClass.service.InquiryService;
import com.zr.util.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * Created by 非常狂躁的猪 on 2019/12/26.
 */
@Service
public class InquiryServiceImpl implements InquiryService {

    @Autowired
    private InquiryMapper inquiryMapper;

    @Override
    public ResultVo queryAllInquiry(InquiryQueryAllVo inquiryQueryAllVo) {
        List<InquiryTotal> list = inquiryMapper.queryAllInquiry(inquiryQueryAllVo);
        int count = inquiryMapper.queryAllInquiryCount(inquiryQueryAllVo);

        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(list);
        allRecords.setPageSize(inquiryQueryAllVo.getPageSize());
        allRecords.setPageIndex(inquiryQueryAllVo.getPageIndex());
        allRecords.setTotalNumber(count); //总共的条数
        allRecords.resetTotalNumber(count); //计算总页数
        return ResultBuilderVo.success(allRecords);
    }

    @Override
    public ResultVo getPartner(Integer legalPersonId) {
        if (legalPersonId == null){
            return ResultBuilderVo.error("法人id不能为空");
        }
        List<SupplierVo> list = inquiryMapper.getAllSupplier(legalPersonId);

        return ResultBuilderVo.success(list);
    }

    @Override
    public ResultVo getUserDanHao() {
        InquiryTotal inquiryTotal = new InquiryTotal();
        inquiryTotal.setCreatorName("宫本"); //询价人员
        Date date = new Date();
        inquiryTotal.setCreateTime(date); //询价日期(创建时间)
        String dingDanHao = CreateDingDan.getNowDateSdf();
        inquiryTotal.setRequestForQuotationId(dingDanHao); //订单号

        return ResultBuilderVo.success(inquiryTotal);
    }

    @Override
    public ResultVo selectMaterial(SpareQueryAllVo spareQueryAllVo) {

        List<SpareVo> list = inquiryMapper.queryAllSpareVo(spareQueryAllVo);
        List<SpareQueryAllVo> list2 = new ArrayList<>();


        for (SpareVo s : list){
            SpareQueryAllVo spareQueryAllVo2 = new SpareQueryAllVo();
            spareQueryAllVo2.setCode(s.getSpareCode());
            spareQueryAllVo2.setId(s.getId());
            spareQueryAllVo2.setName(s.getSpareName());
            spareQueryAllVo2.setMeasurementUnit(s.getMeasurementUnit());
            list2.add(spareQueryAllVo2);
        }
        int count = inquiryMapper.queryAllSpareVoCount(spareQueryAllVo);
     //  System.out.println(list2);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(list2);
        allRecords.setPageSize(spareQueryAllVo.getPageSize());
        allRecords.setPageIndex(spareQueryAllVo.getPageIndex());
        allRecords.setTotalNumber(count); //总共的条数
        allRecords.resetTotalNumber(count); //计算总页数
        return ResultBuilderVo.success(allRecords);

    }

    @Override
    public ResultVo searchPartnerPage(SupplierQueryVo supplierQueryVo) {

        List<SupplierVo> list = inquiryMapper.queryAllSupplier(supplierQueryVo);
        int count = inquiryMapper.queryAllSupplierCount(supplierQueryVo);

        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(list);
        allRecords.setPageSize(supplierQueryVo.getPageSize());
        allRecords.setPageIndex(supplierQueryVo.getPageIndex());
        allRecords.setTotalNumber(count); //总共的条数
        allRecords.resetTotalNumber(count); //计算总页数

        return ResultBuilderVo.success(allRecords);
    }

    @Override
    @Transactional
    public ResultVo add(InquiryTotal inquiryTotal) {
        ResultVo resultVoPanduan =  panDuan(inquiryTotal); //判断的配件能否被下边所有的供应商提供

        if (resultVoPanduan!=null){
            return resultVoPanduan;
        }

        ResultVo resultVoPanduan2 = panDuanGongYing(inquiryTotal);  //panduanGongYingShang    判断供应商是否存在不提供配件的

        if (resultVoPanduan2!=null){
            return resultVoPanduan2;
        }

        ResultVo resultVoPanduanCongFu = panduanCongFu(inquiryTotal);  //判断是否有重复元素;  panduanCongFu()  供应商和备件

        if (resultVoPanduanCongFu != null ){
            return resultVoPanduanCongFu;
        }

        ResultVo resultVoCunZaiFou = panDuanCunZaiFou(inquiryTotal);
        if (resultVoCunZaiFou != null){
            return resultVoCunZaiFou;
        }


       // ResultVo resultVoCunZaiFou = panDuanCunZaiFou(inquiryTotal);

        Date date = new Date();
        inquiryTotal.setCreateTime(date); //询价日期(创建时间)
        String dingDanHao = CreateDingDan.getNowDateSdf();
        inquiryTotal.setRequestForQuotationId(dingDanHao); //订单号
        inquiryTotal.setCreatorName("宫本"); //询价人员
        inquiryTotal.setCreateTime(date);
        inquiryTotal.setUpdateName("宫本");
        inquiryTotal.setUpdateTime(date);
        inquiryMapper.addInquiryTotal(inquiryTotal); //先添加头表
        InquiryTotal inquiryTotal2 = inquiryMapper.getInquiryId(inquiryTotal.getRequestForQuotationId()); //根据订单号查出头表的id
        //这块可以设置sql语句回显自增id
        for (SpareZongJianVo i:inquiryTotal.getInquiryDetailsList()){
            i.setInquiryId(inquiryTotal2.getId());

        }
       // System.out.println(inquiryTotal.getInquiryDetailsList());
        for (VendorVo v:inquiryTotal.getSupplierDetailsList()){
            v.setInquiryId(inquiryTotal2.getId());

        }

        inquiryMapper.addInquirySparedetail(inquiryTotal.getInquiryDetailsList()); //添加到备份的备件表中
        inquiryMapper.addInquirySupplierdetail(inquiryTotal.getSupplierDetailsList()); //添加到备份的供应商表中

        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo see(String requestForQuotationId) {
        InquiryTotal inquiryTotal = inquiryMapper.getInquiryId(requestForQuotationId);
        List<SpareZongJianVo> spareZongJianVoList = inquiryMapper.getBeiFenSpare(inquiryTotal.getId()); //获取全部的配件备份表
        List<VendorVo> vendorVoList = inquiryMapper.getBeiFenGongYingShang(inquiryTotal.getId());
        inquiryTotal.setInquiryDetailsList(spareZongJianVoList);
        inquiryTotal.setSupplierDetailsList(vendorVoList);

        List<InquiryTotal> list = new ArrayList<>();
        list.add(inquiryTotal);

        return ResultBuilderVo.success(list);
    }

    @Override
    @Transactional
    public ResultVo update(InquiryTotal inquiryTotal) {
        ResultVo resultVoPanduan =  panDuan(inquiryTotal); //判断的配件能否被下边所有的供应商提供
        if (resultVoPanduan!=null){
            return resultVoPanduan;
        }

        ResultVo resultVoPanduan2 = panDuanGongYing(inquiryTotal);  //panduanGongYingShang    判断供应商是否存在不提供配件的

        if (resultVoPanduan2!=null){
            return resultVoPanduan2;
        }

        InquiryTotal inquiryTotal3 = inquiryMapper.getInquiryId(inquiryTotal.getRequestForQuotationId()); //根据订单号查出头表的id
        inquiryMapper.deleteInquiryTotal(inquiryTotal.getRequestForQuotationId()); //删除表头
        inquiryMapper.deleteSparedetail(inquiryTotal3.getId());
        inquiryMapper.deleteSupplierdetail(inquiryTotal3.getId());

        Date date = new Date();
        inquiryTotal.setCreateTime(date); //询价日期(创建时间)
        //String dingDanHao = CreateDingDan.getNowDateSdf();
        inquiryTotal.setRequestForQuotationId(inquiryTotal.getRequestForQuotationId()); //订单号
        inquiryTotal.setCreatorName("宫本"); //询价人员
        inquiryTotal.setCreateTime(date);
        inquiryTotal.setUpdateName("宫本");
        inquiryTotal.setUpdateTime(date);
        inquiryMapper.addInquiryTotal(inquiryTotal); //先添加头表
        InquiryTotal inquiryTotal2 = inquiryMapper.getInquiryId(inquiryTotal.getRequestForQuotationId()); //根据订单号查出头表的id

        for (SpareZongJianVo i:inquiryTotal.getInquiryDetailsList()){
            i.setInquiryId(inquiryTotal2.getId());

        }
        // System.out.println(inquiryTotal.getInquiryDetailsList());
        for (VendorVo v:inquiryTotal.getSupplierDetailsList()){
            v.setInquiryId(inquiryTotal2.getId());

        }

        inquiryMapper.addInquirySparedetail(inquiryTotal.getInquiryDetailsList()); //添加到备份的备件表中
        inquiryMapper.addInquirySupplierdetail(inquiryTotal.getSupplierDetailsList()); //添加到备份的供应商表中

        return ResultBuilderVo.success();
    }

    @Override
    public ResultVo quotation(MultipartFile file)throws Exception {
    //  1.给用户提供导入模板
    //  2.提示版本问题,询问产品经理支持哪种版本的导入格式,使用2007版本的导入模板,根据版本的不同,poi导入技术的读取方式不同
        if (!file.getOriginalFilename().contains("xlsx")){
            return ResultBuilderVo.error("文件        必须时2007版本的");
        }
    //  3.模板是否是需要导入的模板,验证模板是否正确
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());
        //获取单元格种的信息at0是获取shee1种的数据
        XSSFSheet sheet = workbook.getSheetAt(0);
        System.out.println(sheet.getRow(sheet.getFirstRowNum()).getCell(0));
        if (!String.valueOf(sheet.getRow(sheet.getFirstRowNum()).getCell(0)).equals("备件导入模板")){
            return ResultBuilderVo.error("模板错误,请检查模板");
        }
        List<SpareZongJianVo> list = new ArrayList<>();
        //遍历excel表格种的所有数据,从第五行开始读,没有数据时终止.

        for (int i = sheet.getFirstRowNum()+4;i<=sheet.getLastRowNum();i++){
            //读取每一行的数据
            XSSFRow xssfRow = sheet.getRow(i);

            if (xssfRow != null ){

                String test1 = String.valueOf(xssfRow.getCell(0)).trim(); //code
                String test2 = String.valueOf(xssfRow.getCell(1)).trim(); //name
                String test3 = String.valueOf(xssfRow.getCell(2)).trim(); //measurementUnit

                if (!test1.equals("")&&!test1.equals("null")
                       || !test2.equals("")&&!test2.equals("null")
                       || !test3.equals("")&&!test3.equals("null")
                        ){
                    //把每一行的数据放入到实体类中
                    ResultVo<SpareZongJianVo> inquiryImportVo = build(xssfRow);
                    if (!inquiryImportVo.getSuccess()){
                        return inquiryImportVo;
                    }
                    //把实体类中的数据放入到list集合中
                    list.add(inquiryImportVo.getData());

                }

            }
        }

        ResultVo resultVoCongFu = panduanBeiJianCongFu(list); //判数据是否重复
        if (resultVoCongFu!= null){
            return resultVoCongFu;
        }

        ResultVo resultVoPanDuanShuJuChangDu = PanDuanShuJuChangDu(list); //判断数据长度是否合法
        if (resultVoPanDuanShuJuChangDu!= null){
            return resultVoPanDuanShuJuChangDu;
        }

        ResultVo resultVo =  panduanBeiJianCunZAIfou(list); //数据是否存在或者编码名字是否正确
        if (resultVo!= null){
            return resultVo;
        }



        return ResultBuilderVo.success(list);
    }

    private ResultVo<SpareZongJianVo> build(XSSFRow xssfRow) {
        SpareZongJianVo spareZongJianVo = new SpareZongJianVo();
        //4.格式问题,必须保持统一格式,统一为字符串格式导入
        spareZongJianVo.setSpareCoding(String.valueOf(xssfRow.getCell(0)));
        spareZongJianVo.setSpareName(String.valueOf(xssfRow.getCell(1)));
        spareZongJianVo.setMeasurementUnit(String.valueOf(xssfRow.getCell(2)));
        return ResultBuilderVo.success(spareZongJianVo);
    }

    public ResultVo PanDuanShuJuChangDu(List<SpareZongJianVo> list ){

        for (SpareZongJianVo spare:list){
            if (spare.getSpareCoding().length()>50){
                return ResultBuilderVo.error("编码长度不合法");
            }
            if (spare.getSpareName().length()>50){
                return ResultBuilderVo.error("名称长度不合法");
            }


        }

        return null;
    }

    public ResultVo panduanBeiJianCongFu(List<SpareZongJianVo> list){
        Set<SpareZongJianVo> set = new HashSet<>(list);
        if (set.size() != list.size()){
            return  ResultBuilderVo.error("存在重复的编码");
        }
        return null;
    }

    public ResultVo panduanBeiJianCunZAIfou(List<SpareZongJianVo> list){
        List<SpareVo> zhenshi = inquiryMapper.panduanBeiJianCunZAIfou(list);

        for (int i = 0;i<list.size();i++){
            int a = 10; int b = 10;String aaa = "";
            for (int j = 0;j<zhenshi.size();j++){
                if (list.get(i).getSpareCoding().equals(zhenshi.get(j).getSpareCode())){
                    a = 1;
                }
                if (list.get(i).getSpareName().equals(zhenshi.get(j).getSpareName())){
                    b = 1;
                }

            }
            if (a>5 && b>5){
                return ResultBuilderVo.error(list.get(i).getSpareName()+"该配件不存在");
            }
            if (a>5 && b<5 ){
                return ResultBuilderVo.error(list.get(i).getSpareName()+"该配件编码不正确");
            }
            if (a<5 && b>5){
                return ResultBuilderVo.error("编码为"+list.get(i).getSpareCoding()+"配件的名字不正确");
            }



        }


        return null;
    }


    public ResultVo panDuan(InquiryTotal inquiryTotal){

        List<String> stringList = new ArrayList<>(); //存放配件名称的集合
        List<SpareAndSupplier> list = inquiryMapper.queryAllSpareAndSupplier(inquiryTotal.getSupplierDetailsList()); //查中间表 根据供应商
        List<String> stringList1Code = new ArrayList<>(); //存放配件编码的集合
        for (int i = 0;i<inquiryTotal.getInquiryDetailsList().size();i++){
            Boolean panduan = false;
            for (int j = 0;j<list.size();j++){
                if (inquiryTotal.getInquiryDetailsList().get(i).getSpareCoding().equals(list.get(j).getSpareCoding())){
                    panduan = true;

                }
            }
            if (panduan == false){
                stringList.add(inquiryTotal.getInquiryDetailsList().get(i).getSpareName());
                stringList1Code.add(inquiryTotal.getInquiryDetailsList().get(i).getSpareCoding());
            }


        }
        String aaa = "";
        if (stringList.size() >0){
            //查出可以生产这些供应商的配件
            List<String > listString  = inquiryMapper.queryChaDeGongYingShang(stringList1Code);
            Set<String> set = new HashSet<>(listString); //对数据进行去重

            for (int i = 0;i<stringList.size();i++){
                aaa = aaa+stringList.get(i)+";";
            }
           // System.out.println(aaa);
            return ResultBuilderVo.error(aaa+"这些配件没有对应的供应商提供"+set+"等供应商可以提供对应的配件");

        }else {
            return  null;
        }


    }

    public ResultVo panDuanGongYing(InquiryTotal inquiryTotal){

        List<SpareAndSupplier> list = inquiryMapper.queryAllSpareAndSupplierByPeiJian(inquiryTotal.getInquiryDetailsList());
        List<String> stringList = new ArrayList<>();
       // System.out.println("aaaa"+inquiryTotal.getSupplierDetailsList());
        for(int i = 0;i<inquiryTotal.getSupplierDetailsList().size();i++){
            Boolean panduan = false;
            for (int j = 0;j<list.size();j++){
                if (inquiryTotal.getSupplierDetailsList().get(i).getVendorCode().equals(list.get(j).getVendorCode())){
                    panduan = true;
                }

            }
            if (panduan == false){
                stringList.add(inquiryTotal.getSupplierDetailsList().get(i).getVendor());
            }

        }

        String aaa = "";
        if (stringList.size() >0){

            for (int i = 0;i<stringList.size();i++){
                aaa = aaa+stringList.get(i)+";";
            }
            return ResultBuilderVo.error(aaa+"等供应商不提供上边任何配件");
        }else {
            return null;
        }


    }

    public ResultVo panduanCongFu(InquiryTotal inquiryTotal){

        List<String> stringList = new ArrayList<>();
        if (inquiryTotal.getInquiryDetailsList().size() >1) {

            for (int i = 0; i < inquiryTotal.getInquiryDetailsList().size(); i++) { //得判断这个集合的长度是否大于一;等于一定话就没必要进行判断了
                for (int j = i + 1; j < inquiryTotal.getInquiryDetailsList().size(); j++) {
                    if (inquiryTotal.getInquiryDetailsList().get(i).getSpareName().equals(inquiryTotal.getInquiryDetailsList().get(j).getSpareName())) {
                        stringList.add(inquiryTotal.getInquiryDetailsList().get(i).getSpareName());
                    }

                }

            }

        }

        if (stringList.size()>0){
            return ResultBuilderVo.error(stringList+"这些备件存在重复的");
        }

        if (inquiryTotal.getSupplierDetailsList().size()>1) {

            for (int i = 0; i < inquiryTotal.getSupplierDetailsList().size(); i++) {
                for (int j = i + 1; j < inquiryTotal.getSupplierDetailsList().size(); j++) {
                    if (inquiryTotal.getSupplierDetailsList().get(i).getVendor().equals(inquiryTotal.getSupplierDetailsList().get(j).getVendor())) {
                        stringList.add(inquiryTotal.getSupplierDetailsList().get(i).getVendor());
                    }
                }

            }

        }

        if (stringList.size()>0){
            return ResultBuilderVo.error(stringList+"这些供应商存在重复数据");
        }else {
            return  null;
        }


    }

    public ResultVo panDuanCunZaiFou(InquiryTotal inquiryTotal){

       List<String> stringList = new ArrayList<>();
       List<SpareVo> zongJianVoList = inquiryMapper.queryAllSpareVoByNoTiaoJian();

       for (int i = 0;i<inquiryTotal.getInquiryDetailsList().size();i++){
           Boolean panduan = false;
           for (int j = 0;j<zongJianVoList.size();j++){
               if (inquiryTotal.getInquiryDetailsList().get(i).getSpareCoding().equals(zongJianVoList.get(j).getSpareCode())){
                   panduan = true;
               }
           }

           if (panduan == false){
               stringList.add(inquiryTotal.getInquiryDetailsList().get(i).getSpareName());
           }

       }

       if (stringList.size()>0){
           return ResultBuilderVo.error(stringList+"这些备件不存在");
       }

       List<SupplierVo> supplierVoList = inquiryMapper.queryAllSupplierNoTiaoJian();
       for (int i = 0;i <inquiryTotal.getSupplierDetailsList().size();i++){
           Boolean panduan = false;
           for (int j = 0;j<supplierVoList.size();j++){
                if (inquiryTotal.getSupplierDetailsList().get(i).getVendorCode().equals(supplierVoList.get(j).getPartnerCode())){
                    panduan = true;
                }


           }

           if (panduan == false ){
               stringList.add(inquiryTotal.getSupplierDetailsList().get(i).getVendor());
           }


       }

       if (stringList.size()>0){
           return ResultBuilderVo.error(stringList+"这些供应商不存在");
       }else {
           return null;
       }


    }





}
























































